def extract_gui():
    def extract_data():
        input_text = input_textbox.get("1.0", tk.END)
        command_name = command_entry.get().strip()
        header_value_str = header_entry.get().strip()
        index_start_str = index_entry.get().strip()
        start_find_value = start_find_entry.get()
        end_find_value = end_find_entry.get()
        include_non_hex = include_non_hex_var.get()
        use_alternative_rule = alternative_rule_var.get()

        # 获取数据分隔符
        if use_custom_separator_var.get():
            separator = separator_entry.get() or ',;'  # 默认为逗号
        else:
            separator = ','  # 默认分隔符为逗号

        # 读取并处理 skip_data
        try:
            skip_data = int(skip_data_entry.get().strip()) if skip_data_var.get() else 0
        except ValueError:
            skip_data = 0

        if not command_name:
            command_name = "GEN_WR"
        if not start_find_value:
            start_find_value = "("
        if not end_find_value:
            end_find_value = ")"

        if header_value_str.startswith("0x"):
            header_value = int(header_value_str, 16)
        else:
            header_value = None

        if index_start_str.isdigit():
            index_start = int(index_start_str)
        else:
            index_start = 0

        extracted_data, output_text = extract_specific_command_data(
            input_text, command_name, header_value, index_start, start_find_value, end_find_value, include_non_hex,
            use_alternative_rule, skip_data, separator
        )

        output_textbox.delete("1.0", tk.END)
        for data in extracted_data:
            output_textbox.insert(tk.END, data + "\n")
        output_textbox.insert(tk.END, output_text + "\n")

    extract_window = tk.Toplevel()
    extract_window.title("提取指定命令数据")

    paned_window = tk.PanedWindow(extract_window, orient=tk.HORIZONTAL, sashrelief=tk.RAISED, sashwidth=5)
    paned_window.pack(fill=tk.BOTH, expand=True)

    left_frame = tk.Frame(extract_window)
    right_frame = tk.Frame(extract_window)

    paned_window.add(left_frame)
    paned_window.add(right_frame)

    input_label = tk.Label(left_frame, text="请粘贴要处理的命令文本:")
    input_label.pack(fill=tk.X, padx=5, pady=5)

    input_textbox = scrolledtext.ScrolledText(left_frame, width=40, height=15)
    input_textbox.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)

    command_label = tk.Label(left_frame, text="请输入要提取的命令名称（例如：GEN_WR）： 留空则默认为GEN_WR")
    command_label.pack(fill=tk.X, padx=5, pady=5)

    command_entry = tk.Entry(left_frame)
    command_entry.pack(fill=tk.X, padx=5, pady=5)

    header_label = tk.Label(left_frame, text="请输入指令头的十六进制数值（例如：0x29），留空则默认为0x29:")
    header_label.pack(fill=tk.X, padx=5, pady=5)

    header_entry = tk.Entry(left_frame)
    header_entry.pack(fill=tk.X, padx=5, pady=5)

    index_label = tk.Label(left_frame, text="请输入起始索引值（例如：0），默认为0:")
    index_label.pack(fill=tk.X, padx=5, pady=5)

    index_entry = tk.Entry(left_frame)
    index_entry.pack(fill=tk.X, padx=5, pady=5)

    start_find_label = tk.Label(left_frame, text="请输入有效数据最左边的符号 查找的值（例如：'GEN_WR(0xFF,0x80,0x57) 则输入(或则R(，）默认为'(':")
    start_find_label.pack(fill=tk.X, padx=5, pady=5)

    start_find_entry = tk.Entry(left_frame)
    start_find_entry.pack(fill=tk.X, padx=5, pady=5)

    end_find_label = tk.Label(left_frame, text="请输入有效数据最右边的符号 查找的值（例如：'GEN_WR(0xFF,0x80,0x57) 则输入)或则;，）默认为')':")
    end_find_label.pack(fill=tk.X, padx=5, pady=5)

    end_find_entry = tk.Entry(left_frame)
    end_find_entry.pack(fill=tk.X, padx=5, pady=5)

    # 创建一个框架用于放置 Checkbutton 控件、跳过数据的 Entry 控件和分隔符文本框
    options_frame = tk.Frame(left_frame)
    options_frame.pack(fill=tk.X, padx=5, pady=5)

    include_non_hex_var = tk.BooleanVar()
    include_non_hex_check = tk.Checkbutton(options_frame, text="包含不以 '0x' 开头的数据", variable=include_non_hex_var)
    include_non_hex_check.pack(side=tk.LEFT, padx=5)

    alternative_rule_var = tk.BooleanVar()
    alternative_rule_check = tk.Checkbutton(options_frame, text="使用其他规则", variable=alternative_rule_var)
    alternative_rule_check.pack(side=tk.LEFT, padx=5)

    skip_data_var = tk.BooleanVar()
    skip_data_check = tk.Checkbutton(options_frame, text="跳过第N个数据", variable=skip_data_var)
    skip_data_check.pack(side=tk.LEFT, padx=5)

    skip_data_entry = tk.Entry(options_frame, width=5, state=tk.DISABLED)
    skip_data_entry.pack(side=tk.LEFT, padx=5)

    use_custom_separator_var = tk.BooleanVar()
    use_custom_separator_check = tk.Checkbutton(options_frame, variable=use_custom_separator_var)
    use_custom_separator_check.pack(side=tk.LEFT, padx=5)

    separator_label = tk.Label(options_frame, text="自定义有效数据分隔符（例如：',', ';'）：")
    separator_label.pack(side=tk.LEFT, padx=5)

    separator_entry = tk.Entry(options_frame, width=5)
    separator_entry.pack(side=tk.LEFT, padx=5)

    # 当勾选“跳过第N个数据”时，启用 skip_data_entry 控件并设置默认值为2
    def toggle_skip_data():
        if skip_data_var.get():
            skip_data_entry.config(state=tk.NORMAL)
            if skip_data_entry.get().strip() == '':
                skip_data_entry.insert(0, '2')
        else:
            skip_data_entry.config(state=tk.DISABLED)
            skip_data_entry.delete(0, tk.END)  # 清除内容以避免不必要的显示

    skip_data_check.config(command=toggle_skip_data)

    # 当勾选“使用自定义分隔符”时，启用 separator_entry 控件，否则禁用它
    def toggle_separator_entry():
        if use_custom_separator_var.get():
            separator_entry.config(state=tk.NORMAL)
        else:
            separator_entry.config(state=tk.DISABLED)
            separator_entry.delete(0, tk.END)  # 清除内容以避免不必要的显示

    use_custom_separator_check.config(command=toggle_separator_entry)

    extract_button = tk.Button(left_frame, text="提取数据", command=extract_data)
    extract_button.pack(pady=10)

    output_label = tk.Label(right_frame, text="提取结果:")
    output_label.pack(fill=tk.X, padx=5, pady=5)

    output_textbox = scrolledtext.ScrolledText(right_frame, width=40, height=15)
    output_textbox.pack(fill=tk.BOTH, expand=True, padx=5, pady=5)

    extract_window.mainloop()